import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { UserService, BaseQueryParameter } from '../../service/user.service';
import { Toastr } from '../../common/toastr';
import { CircleFriend } from '../../domain/circleFriend';
import { PagerComponent } from '../../component/pager/pager.component';

@Component({
    moduleId: module.id,
    templateUrl: 'circleFriend.component.html'
})
export class CircleFriendComponent implements OnInit {
    private title: string = "朋友圈信息";
    private subTitle: string = "查询朋友圈信息";
    private queryParams: BaseQueryParameter = {
        currentPage: 1,
        pageSize: 10
    };
    private flist: CircleFriend[] = [];
    @ViewChild(PagerComponent)
    private pager: PagerComponent;
    constructor(
        private route: ActivatedRoute,
        private userSvr: UserService
    ) { }

    ngOnInit() {
        this.getCircleFriendList();
        this.pager.OnPageIndexChanged = (pageIndex: number) => {
            this.queryParams.currentPage = pageIndex;
            this.getCircleFriendList();
        }
    }

    async getCircleFriendList() {
        try {
            let r = await this.userSvr.GetCircleFriendList(this.queryParams);
            this.pager.Load(r.count);
            this.flist = r.list;
        } catch (error) {
            Toastr.Error(error);
        }
    }

    formatPic(pic: string) {
        if (pic) {
            let list = pic.split(',');
            return list;
        }
        else return [];
    }

    async pass(item: CircleFriend) {
        try {
            await this.userSvr.VerifyCircle(item.itemId, 1);
            this.getCircleFriendList();
            Toastr.Success("操作成功！");
        } catch (error) {

        }
    }

    async unpass(item: CircleFriend) {
        try {
            await this.userSvr.VerifyCircle(item.itemId, 2);
            this.getCircleFriendList();
            Toastr.Success("操作成功！");
        } catch (error) {
            Toastr.Error(error);
        }
    }
}